#include <iostream>
#include <fstream>

#include "ros/ros.h"
#include "demo/Camera.h"

int main(int argc, char* argv[]) {
    std::cout << "Hello Server!!!" << std::endl;
    ros::init(argc, argv, "server");
    ros::NodeHandle nodeHandle;
    const std::string topicName = "demo9";
    uint32_t queueSize = 1000;
    const ros::Publisher &publisher = nodeHandle.advertise<demo::Camera>(topicName, queueSize);
    ros::Rate rate(1);
    const std::string path = "./src/demo/data/server.png";
    std::ifstream file(path, std::ios::binary);
    if (!file.is_open()) {
        ROS_ERROR("Failed to file: %s", path.c_str());
        return EXIT_FAILURE;
    }
    file.seekg(0, std::ios::end);
    size_t size = file.tellg();
    file.seekg(0, std::ios::beg);
    std::vector<uint8_t> data(size);
    file.read(reinterpret_cast<char *>(&data[0]), size);
    file.close();
    while (ros::ok()) {
        demo::Camera msg;
        msg.width = 1920;
        msg.height = 1080;
        msg.data = data;
        publisher.publish(msg);
        ROS_INFO("size: %ld", size);
        rate.sleep();
    }

    return EXIT_SUCCESS;
}